// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.api;

/**
 * 文件服务器api
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2022年2月25日
 */

public class FileRequestService {
//    private UserInfo _userInfo;
//    private String _url;
//    /// <summary>
//    /// 获取站点contextInfo信息
//    /// </summary>
//    /// <returns></returns>
//    public String GetContextinfo()
//    {
//        HttpRequest contextInfoRequest = null;
//        HttpResponse endpointResponse = null;
//        StreamReader sr = null;
//        String strJson = String.Empty;
//        try
//        {
//            //获取contextinfo
//            contextInfoRequest = (HttpRequest)HttpWebRequest.Create(_url + "/_api/contextinfo");
//            contextInfoRequest.Method = "POST";
//            contextInfoRequest.Credentials = new NetworkCredential(_userInfo.Itcode, _userInfo.Pwd, _userInfo.Doamin);
//            contextInfoRequest.Accept = "application/json;odata=verbose";
//            contextInfoRequest.ContentLength = 0;
//            using (endpointResponse = (HttpWebResponse)contextInfoRequest.GetResponse())
//            {
//                using (sr = new StreamReader(endpointResponse.GetResponseStream(), Encoding.UTF8))
//                {
//                    strJson = sr.ReadToEnd();
//                    JObject jobj = JObject.Parse(strJson);
//                    return jobj["d"]["GetContextWebInformation"]["FormDigestValue"].ToString();
//                }
//            }
//        }
//        catch (Exception ex)
//        {
//            throw ex;
//        }
//    }
//    /// <summary>
//    /// 创建文件夹
//    /// </summary>
//    /// <param name="strName">要创建文件夹的相对路径</param>
//    /// <returns></returns>
//    public String CreateFolder(String serverRelativeUrl)
//    {
//        String strJson = String.Empty;
//        Uri hostWebUri = new Uri(_url);
//        HttpWebResponse response = null;
//        StreamReader sr = null;
//        HttpWebRequest request = null;
//        try
//        {
//            String metadata = "{'__metadata': { 'type': 'SP.Folder' },'ServerRelativeUrl':'" + serverRelativeUrl + "'}";
//            byte[] buffer = Encoding.UTF8.GetBytes(metadata);
//            request = (HttpWebRequest)HttpWebRequest.Create(_url + "/_api/web/folders");
//            request.Method = "POST";
//            request.Credentials = new NetworkCredential(_userInfo.Itcode, _userInfo.Pwd, _userInfo.Doamin);
//            request.Accept = "application/json;odata=verbose";
//            request.ContentLength = buffer.Length;
//            request.ContentType = "application/json;odata=verbose";
//            request.Headers.Add("X-RequestDigest", this.GetContextinfo());
//            using (Stream requestStream = request.GetRequestStream())
//            {
//                requestStream.Write(buffer, 0, buffer.Length);
//            }
//            using (response = (HttpWebResponse)request.GetResponse())
//            {
//                using (sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
//                {
//                    strJson = sr.ReadToEnd();
//                }
//            }
//        }
//        catch (WebException ex)
//        {
//            throw ex;
//        }
//        return strJson;
//    }
//    /// <summary>
//    /// 修改文件夹
//    /// </summary>
//    /// <param name="strAPI"></param>
//    /// <returns></returns>
//    public String UpdateFolder(String folderRelativeUrl, String strName)
//    {
//        String strJson = String.Empty;
//        Uri hostWebUri = new Uri(_url);
//        HttpWebResponse response = null;
//        StreamReader sr = null;
//        HttpWebRequest request = null;
//        try
//        {
//            String metadata = "{'__metadata': {'type': 'SP.Folder'},'Name':'" + strName + "','ServerRelativeUrl':'" + folderRelativeUrl + "'}";
//            byte[] buffer = Encoding.UTF8.GetBytes(metadata);
//            request = (HttpWebRequest)HttpWebRequest.Create(_url + "/_api/web/GetFolderByServerRelativeUrl('" + folderRelativeUrl + "')");
//            request.Method = "POST";
//            request.Credentials = new NetworkCredential(_userInfo.Itcode, _userInfo.Pwd, _userInfo.Doamin);
//            request.Accept = "application/json;odata=verbose";
//            request.ContentLength = buffer.Length;
//            request.ContentType = "application/json;odata=verbose";
//            request.Headers.Add("IF-MATCH", "*");
//            request.Headers.Add("X-HTTP-Method", "MERGE");
//            request.Headers.Add("X-RequestDigest", this.GetContextinfo());
//            using (Stream requestStream = request.GetRequestStream())
//            {
//                requestStream.Write(buffer, 0, buffer.Length);
//            }
//            using (response = (HttpWebResponse)request.GetResponse())
//            {
//                using (sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
//                {
//                    strJson = sr.ReadToEnd();
//                }
//            }
//        }
//        catch (WebException ex)
//        {
//            throw ex;
//        }
//        return strJson;
//    }
//    /// <summary>
//    /// 移除文件
//    /// </summary>
//    /// <param name="fileRelativeUrl"></param>
//    /// <returns></returns>
//    public String RemoveFile(String fileRelativeUrl)
//    {
//        String strJson = String.Empty;
//        Uri hostWebUri = new Uri(_url);
//        HttpWebResponse response = null;
//        StreamReader sr = null;
//        HttpWebRequest request = null;
//        try
//        {
//            request = (HttpWebRequest)HttpWebRequest.Create(_url + "/_api/web/GetFileByServerRelativeUrl('" + fileRelativeUrl + "')");
//            request.Method = "POST";
//            request.Credentials = new NetworkCredential(_userInfo.Itcode, _userInfo.Pwd, _userInfo.Doamin);
//            request.Accept = "application/json;odata=verbose";
//            request.ContentLength = 0;
//            request.ContentType = "application/json;odata=verbose";
//            request.Headers.Add("IF-MATCH", "*");
//            request.Headers.Add("X-HTTP-Method", "DELETE");
//            request.Headers.Add("X-RequestDigest", this.GetContextinfo());
//            using (response = (HttpWebResponse)request.GetResponse())
//            {
//                using (sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
//                {
//                    strJson = sr.ReadToEnd();
//                }
//            }
//        }
//        catch (WebException ex)
//        {
//            throw ex;
//        }
//        return strJson;
//    }
//    /// <summary>
//    /// 文件移动或者拷贝
//    /// </summary>
//    /// <param name="strAPi"></param>
//    /// <returns></returns>
//    public String CopyOrMoveTo(String strAPi)
//    {
//        String strJson = String.Empty;
//        Uri hostWebUri = new Uri(_url);
//        HttpWebResponse response = null;
//        StreamReader sr = null;
//        HttpWebRequest request = null;
//        try
//        {
//            request = (HttpWebRequest)HttpWebRequest.Create(_url + "/_api/web/" + strAPi);
//            request.Method = "POST";
//            request.Credentials = new NetworkCredential(_userInfo.Itcode, _userInfo.Pwd, _userInfo.Doamin);
//            request.Accept = "application/json;odata=verbose";
//            request.ContentLength = 0;
//            request.ContentType = "application/json;odata=verbose";
//            using (response = (HttpWebResponse)request.GetResponse())
//            {
//                using (sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
//                {
//                    strJson = sr.ReadToEnd();
//                }
//            }
//        }
//        catch (WebException ex)
//        {
//            throw ex;
//        }
//        return strJson;
//    }
//    /// <summary>
//    /// 上传文件
//    /// </summary>
//    /// <param name="strApi"></param>
//    /// <param name="data"></param>
//    /// <returns></returns>
//    public String PostFile(String strApi, byte[] data)
//    {
//        String strJson = String.Empty;
//        Uri hostWebUri = new Uri(_url);
//        HttpWebResponse fielResponse = null;
//        StreamReader sr = null;
//        HttpWebRequest fileRequest = null;
//        try
//        {
//            fileRequest = (HttpWebRequest)HttpWebRequest.Create(_url + "/_api/web/" + strApi);
//            fileRequest.Method = "POST";
//            fileRequest.Credentials = new NetworkCredential(_userInfo.Itcode, _userInfo.Pwd, _userInfo.Doamin);
//            fileRequest.Accept = "application/json;odata=verbose";
//            fileRequest.ContentLength = data.Length;
//            fileRequest.Headers.Add("X-RequestDigest", this.GetContextinfo());
//            using (Stream requestStream = fileRequest.GetRequestStream())
//            {
//                requestStream.Write(data, 0, data.Length);
//            }
//            using (fielResponse = (HttpWebResponse)fileRequest.GetResponse())
//            {
//                using (sr = new StreamReader(fielResponse.GetResponseStream(), Encoding.UTF8))
//                {
//                    strJson = sr.ReadToEnd();
//                }
//            }
//        }
//        catch (WebException ex)
//        {
//            throw ex;
//        }
//        return strJson;
//    }
//    /// <summary>
//    /// 下载文件
//    /// </summary>
//    /// <param name="apiUrl">下载文件的api</param>
//    /// <param name="filePath">在服务端保存的路径</param>
//    /// <returns></returns>
//    public void DownLoadFile(String apiUrl, String filePath)
//    {
//        byte[] buffer = null;
//        Uri hostWebUri = new Uri(_url);
//        try
//        {
//            //下载的时候避免重名文件进行覆盖
//            if (File.Exists(filePath))
//            {
//                File.Delete(filePath);
//            }
//            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(_url + "/_api/web/" + apiUrl);
//            request.Method = "GET";
//            request.Credentials = new NetworkCredential(_userInfo.Itcode, _userInfo.Pwd, _userInfo.Doamin);
//            request.Accept = "application/json;odata=verbose";
//            request.ContentType = "application/octet-stream";
//            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
//            {
//                using (Stream stream = response.GetResponseStream())
//                {
//                    using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
//                    {
//                        int bufferSize = 1024;
//                        //缓冲
//                        buffer = new byte[bufferSize];
//                        //真实读取的大小
//                        int length = stream.Read(buffer, 0, bufferSize);
//                        while (length > 0)
//                        {
//                            fs.Write(buffer, 0, length);
//                            length = stream.Read(buffer, 0, bufferSize);
//                        }
//                    }
//                }
//            }
//        }
//        catch (Exception ex)
//        {
//            throw ex;
//        }
//    }
//    /// <summary>
//    /// get请求
//    /// </summary>
//    /// <returns></returns>
//    public String RequestGet(String strApi)
//    {
//        String strJson = String.Empty;
//        Uri hostWebUri = new Uri(_url);
//        HttpWebRequest endpointRequest = null;
//        HttpWebResponse endpointResponse = null;
//        StreamReader sr = null;
//        try
//        {
//            strApi = _url + "/_api/web/" + strApi;
//            endpointRequest = (HttpWebRequest)HttpWebRequest.Create(strApi);
//            endpointRequest.Credentials = new NetworkCredential(_userInfo.Itcode, _userInfo.Pwd, _userInfo.Doamin);
//            endpointRequest.Method = "GET";
//            endpointRequest.Accept = "application/json;odata=verbose";
//            using (endpointResponse = (HttpWebResponse)endpointRequest.GetResponse())
//            {
//                using (sr = new StreamReader(endpointResponse.GetResponseStream(), Encoding.UTF8))
//                {
//                    strJson = sr.ReadToEnd();
//                }
//            }
//        }
//        catch (Exception ex)
//        {
//            throw ex;
//        }
//        return strJson;
//    }
//}
}
